package ah.html;

import html.entity.JiBing;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;

/**
 * parse JiBing page
 * @author dingdahai
 *
 */
public class Parser2 extends ParserCallback {
	protected String base;
	protected boolean isLink = false;
	protected boolean isParagraph = false;
	protected boolean isTitle = false;
	protected String htmlbody = new String();
	protected String urlTitle = new String();
	protected Vector<String> links = new Vector<String>();
	protected Vector<String> linkname = new Vector<String>();
	protected String paragraphText = new String();
	protected String linkandparagraph = new String();
	protected String encode = new String();

	protected boolean isInDiv_jb_title = false;//ddh 是否在需要的div （class为 jb_title ）内
	protected boolean isDiv_jb_title_h1 = false;
	protected boolean isDiv_jb_title_h1_span = false;
	protected JiBing jiBing = new JiBing();//疾病保存在此对象里
	
	protected boolean divHasAppear_channel = false;//此div是否出现了 （class）
	protected boolean isAfterDiv_channel_UL = false;//是否是div后的UL
	protected boolean isAfterDiv_channel_UL_LI = false;
	protected boolean isPartStart = false;//部位
	protected boolean isKeshiStart = false;//科室
	protected boolean is_channel_li_a = false;
	protected StringBuilder part = null;
	protected StringBuilder keshi = null;
	
	
	
	public Parser2(String baseurl) {
		base = baseurl;
	}

	public String getEncode() {
		return encode;
	}

	public String getURLtille() {
		return urlTitle;
	}

	public Vector getLinks() {
		return links;
	}

	public Vector getLinkName() {
		return linkname;
	}

	public String getParagraphText() {
		return paragraphText;
	}

	public String getLinknameAndParagraph() {
		return linkandparagraph;
	}
	

	public void handleComment(char[] data, int pos) {
	}

	public void handleEndTag(HTML.Tag t, int pos) {
		if (t == HTML.Tag.DIV) {
			isInDiv_jb_title = false;
		}
		if (t == HTML.Tag.H1) {
			isDiv_jb_title_h1 = false;
		}
		if (t == HTML.Tag.SPAN) {
			isDiv_jb_title_h1_span = false;
		}
		if (t == HTML.Tag.UL) {
			isAfterDiv_channel_UL = false;
			divHasAppear_channel = false;
		}
		if (t == HTML.Tag.LI) {
			isAfterDiv_channel_UL_LI = false;
			
			if (isKeshiStart) {
				isKeshiStart = false;
				if (keshi.length()>0 && "@".equals(keshi.substring(keshi.length()-1, keshi.length()))) {
					jiBing.setKeshi(keshi.toString().substring(0,keshi.length()-1));
				}
			}
			if (isPartStart) {
				isPartStart = false;
				if (part.length()>0 && "@".equals(part.substring(part.length()-1, part.length()))) {
					jiBing.setPart(part.toString().substring(0, part.length()-1));
				}
			}
		}
		
		if (t == HTML.Tag.A) {
			is_channel_li_a = false;
			if (isLink) {
				isLink = false;
			}
		} else if (t == HTML.Tag.P) {
			if (isParagraph) {
				isParagraph = false;
			}
		} else if (t == HTML.Tag.TITLE) {
			isTitle = false;
		} else if (t == HTML.Tag.AREA) {
			isLink = false;
		}
	}

	public void handleError(String errorMsg, int pos) {
	}

	public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
		handleStartTag(t, a, pos);
	}

	public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
		// is it some sort of a link
		//标题div
		if ( t == HTML.Tag.DIV && "jb_title".equals(a.getAttribute(HTML.Attribute.CLASS)) ) {
			isInDiv_jb_title = true;
		} 
		if (isInDiv_jb_title==true && t==HTML.Tag.H1) {
			isDiv_jb_title_h1 = true;
		}
		if (isDiv_jb_title_h1==true && t==HTML.Tag.SPAN) {
			isDiv_jb_title_h1_span = true;
		}
		
		if (t==HTML.Tag.DIV && "channel".equals(a.getAttribute(HTML.Attribute.CLASS))) {
			divHasAppear_channel = true;
		}
		if (divHasAppear_channel && t==HTML.Tag.UL) {
			isAfterDiv_channel_UL = true;
		}
		if (isAfterDiv_channel_UL && t==HTML.Tag.LI) {
			isAfterDiv_channel_UL_LI = true;
		}
		if (isAfterDiv_channel_UL_LI && t==HTML.Tag.A) {
			is_channel_li_a = true;
		}
		/*String href = "";
		if ((t == HTML.Tag.A) && (t != HTML.Tag.BASE)) {
			href = (String) a.getAttribute(HTML.Attribute.HREF);
			if (href != null) {
				try {
					URL url = new URL(new URL(base), href);
					links.addElement(url.toString());
					isLink = true;
				} catch (MalformedURLException e) {
					// System.out.println(e.getMessage());
				}
			}
		} else if (t == HTML.Tag.AREA) {
			href = (String) a.getAttribute(HTML.Attribute.HREF);
			if (href != null) {
				String alt = (String) a.getAttribute(HTML.Attribute.ALT);
				try {
					URL url = new URL(new URL(base), href);
					links.addElement(url.toString());
					if (alt != null) {
						linkname.addElement(alt);
						linkandparagraph += alt;
					}
					isLink = true;
				} catch (MalformedURLException e) {
					// System.out.println(e.getMessage());
				}
			}
		} else if (t == HTML.Tag.TITLE) {
			isTitle = true;
		} else if (t == HTML.Tag.P) {
			isParagraph = true;
		} else if (t == HTML.Tag.BASE) {
			href = (String) a.getAttribute(HTML.Attribute.HREF);
			if (href != null)
				base = href;
		}*/
	}

	public void handleText(char[] data, int pos) {
		if (isLink) {
			String urlname = new String(data);
			
			if (urlname != null) {
				//linkname.addElement(urlname);
				//linkandparagraph += urlname;
				//linksMap.put(urlname, tempLink);
			}
		}
		
		if(isDiv_jb_title_h1 && !isDiv_jb_title_h1_span) {
			jiBing.setName(new String(data));
		}
		if (isDiv_jb_title_h1 && isDiv_jb_title_h1_span) {
			String temp = new String(data);
			if (temp!=null && !"".equals(temp.trim()) && temp.contains("别名")) {
				temp = temp.substring(4, temp.length()-1);
			}
			jiBing.setAlias(temp);
		}
		
		
		if (isAfterDiv_channel_UL_LI && !is_channel_li_a) {
			String temp = new String(data);
			if (temp.contains("科室")) {
				isKeshiStart = true;
				keshi = new StringBuilder();
			} else if (temp.contains("部位")) {
				isPartStart = true;
				part = new StringBuilder();
			}
		}
		if (is_channel_li_a) {
			if (isPartStart && !isKeshiStart) {
				part.append(new String(data)).append("@");
			}
			if (isKeshiStart && !isPartStart) {
				keshi.append(new String(data)).append("@");
			}
		}
		
		
		if (isTitle) {
			String temptitle = new String(data);
			urlTitle = temptitle;
		}
		if (isParagraph) {
			String tempParagraphText = new String(data);
			if (paragraphText != null) {
				paragraphText += tempParagraphText;
				linkandparagraph += tempParagraphText;
			}
		}
	}
}
